package thread;

import java.util.*;

public class SyncApiDemo2 {
    public static void main(String[] args) {
        //List<Integer> list = new ArrayList<>();
        //List<Integer> list = new LinkedList<>();
        //HashSet<Integer> list = new HashSet<>();
        List<Integer> list = Collections.synchronizedList(new ArrayList<>());
        //局部内部类
        Thread t1 = new Thread() {
            public void run() {
                for (int i = 0; i < 1000; i++) {
                    list.add(i);
                }
            }
        };
        Thread t2 = new Thread(){
            public void run() {
                for (int i = 1000; i <2000; i++) {
                    list.add(i);
                }
            }
        };
        t1.start();
        t2.start();
        try {
            t1.join();
            t2.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(list.toArray().length);
    }
}
